package com.pro.controller;

import com.google.gson.Gson;
import com.pro.pojo.JsCourseRecord;
import com.pro.pojo.JsUserCoupon;
import com.pro.pojo.JsUserCouponUsed;
import com.pro.pojo.JsonData;
import com.pro.service.JsCouponsService;
import com.pro.utils.DateUtil;
import com.pro.utils.OrderNoUtil;
import com.pro.utils.RedisCache;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/Coupons")
public class JsCouponsController {
    @Autowired
    private JsCouponsService service;

    @Autowired
    private RedisCache redisCache;
    
    private Gson gson = new Gson();
    /**
     *
     * @param couponName
     * @param online
     * @param grant
     * @param startDate
     * @param endDate
     * @return
     */
    @RequestMapping("/selectCoupons")
    public JsonData selectCoupons (String couponName, String online, String grant, String startDate, String endDate,Integer limit, Integer page){
        couponName = !("").equals(couponName) && couponName !=null?"%"+couponName+"%":"%%";
        online = !("").equals(online) && online !=null?"%"+online+"%":"%%";
        grant = !("").equals(grant) && grant !=null?"%"+grant+"%":"%%";
        if(startDate!=null && startDate!=""){
            startDate ="%"+startDate+"%";
        }else{
            startDate = "%%";
        }

        if(endDate != null && endDate !=""){
            endDate = "%"+endDate+"%";
        }else{
            endDate = "%%";
        }

        List<JsUserCoupon> data = service.selectCoupons(couponName, online, grant,startDate,endDate,limit,page);
        Integer count = service.countCoupons(couponName, online, grant,startDate,endDate);

        //redis缓存
        Gson gson = new Gson();
        String couponsSelectCoupons = redisCache.getCacheObject("CouponsSelectCoupons");
        List list = gson.fromJson(couponsSelectCoupons, List.class);
        Integer integer = redisCache.getCacheObject("CouponsCountCoupons");
        return list!=null? JsonData.buildSuccess(list,integer): JsonData.buildError("服务端错误");
    };

    //通过id查看优惠券详情
    @RequestMapping("/selectCouponById")
    public JsonData selectCouponById(Integer couponId){
        JsUserCoupon jsUserCoupon = service.selectCouponById(couponId);
        return jsUserCoupon!=null? JsonData.buildSuccess(jsUserCoupon): JsonData.buildError("服务端错误");
    };

    //修改优惠券状态
    @RequestMapping("/updateCoupon")
    public JsonData updateCoupon(Integer couponId, Integer online, Integer updateId, String updateDate){
        Boolean aBoolean = service.updateCoupon(couponId, online, updateId, updateDate);
        return aBoolean!=false? JsonData.buildSuccess(aBoolean): JsonData.buildError("服务端错误");
    };

    //删除优惠券
    @RequestMapping("/deleteCoupon")
    public JsonData deleteCoupon(Integer couponId){
        Boolean aBoolean = service.deleteCoupon(couponId);
        return aBoolean!=false? JsonData.buildSuccess(aBoolean): JsonData.buildError("服务端错误");
    };

    //添加优惠券
    @RequestMapping("/addCoupons")
    public JsonData addCoupons(JsUserCoupon jsUserCoupon){
        Boolean aBoolean = service.addCoupons(jsUserCoupon);
        return aBoolean!=false?JsonData.buildSuccess(aBoolean):JsonData.buildError("服务端错误");
    };

    //查询用户优惠券
    @RequestMapping("/selectUserCoupons")
    public JsonData selectUserCoupons(String account,String nickname,String endDate,Integer limit,Integer page){
        account = !("").equals(account) && account !=null?"%"+account+"%":"%%";
        nickname = !("").equals(nickname) && nickname !=null?"%"+nickname+"%":"%%";
        List<JsUserCoupon> data = service.selectUserCoupons(account, nickname,endDate,limit,page);
        Integer integer = service.countUserCoupons(account, nickname, endDate);

        //redis缓存
        String couponsSelectUserCoupons = redisCache.getCacheObject("CouponsSelectUserCoupons");
        List list = gson.fromJson(couponsSelectUserCoupons, List.class);
        Integer integer1 = redisCache.getCacheObject("CouponsCountUserCoupons");
        return list!=null?JsonData.buildSuccess(list,integer1):JsonData.buildError("服务端错误");
    };

    //查看所有优惠券
    @RequestMapping("/selectGiveCoupons")
    public JsonData selectGiveCoupons(String couponName,Integer limit,Integer page){
        couponName = !("").equals(couponName) && couponName !=null?"%"+couponName+"%":"%%";
        List<JsUserCoupon> data = service.selectGiveCoupons(couponName,limit,page);
        Integer count = service.countGiveCoupons(couponName);
        return data!=null?JsonData.buildSuccess(data,count):JsonData.buildError("服务端错误");
    };

    //为所有前台用户添加优惠券
    @RequestMapping("/addBeforeCoupons")
    public JsonData addBeforeCoupons(JsUserCouponUsed jsUserCoupon){
        List<JsUserCouponUsed> listt = new ArrayList<>();
        listt.add(jsUserCoupon);
        Boolean aBoolean = service.addBeforeCoupons(listt);
        return aBoolean!=false?JsonData.buildSuccess(aBoolean):JsonData.buildError("服务端错误");
    };
}
